#coding=utf-8
# Copyright 2012 Maxim Petrov <maximpetrov@yahoo.com>
#
# This software may be used and distributed according to the terms of the
# GNU General Public License version 3 or any later version.

from panel import Panel
from controls import Pad
import curses

class ViewPanel(Panel):
    """
    Panel for show content of file.
    """

    def __init__(self, main_window):
        """
        Constructor of view panel.

        Keyword arguments:
            main_window -- instance of MainWindow
        """

        self._main_window = main_window
        self._file = []
        self._title = None
        self._pad = Pad(main_window, lambda w, h: (1, 1, w - 2, h - 2),
            lambda s, c: ((f.rstrip(), None, None) for f in self._file[s:s+c]))

    def redraw(self, window):
        """
        Function that redraw panel when window for draw changed

        Keyword arguments:
            window -- subwindow for draw panel
        """

        self._window = window
        if not self.visible:
            return
        y, x = window.getbegyx()
        h, w = window.getmaxyx()
        window.subwin(h, w, y, x).box()
        if self._title is not None:
            window.addstr(0, 3, self._main_window.str_to_external(self._title[:w - 4]))
        self._pad.draw(x, y, w, h)
        window.refresh()

    def set_content(self, file, title=None):
        """
        Set content of pad (file for diff)
        """

        self._title = title
        self._file = file
        self.__total_height = len(self._file)
        self.__total_width = max([len(l) for l in self._file] + [0])
        self._pad.x_shift = 0
        self._pad.y_shift = 0
        self._window.refresh()

    def handle_keypress(self, ch, uni):
        """
        Function handles keypresses in panel

        Keyword argument
            ch - code of pressed key or unicode symbol
            uni - True if ch is Unicode char, False otherwise
        """

        if ch == curses.KEY_F12 or ch == 27:
            self._main_window.show_view_panel()
            return
        elif ch == curses.KEY_UP:
            if self._pad.y_shift > 0:
                self._pad.y_shift -= 1
        elif ch == curses.KEY_LEFT:
            if self._pad.x_shift > 0:
                self._pad.x_shift -= 1
        elif ch == curses.KEY_DOWN:
            if self._pad.y_shift + self._pad.height < self.__total_height:
                self._pad.y_shift += 1
        elif ch == curses.KEY_RIGHT:
            if self._pad.x_shift + self._pad.width < self.__total_width:
                self._pad.x_shift += 1
        elif ch == curses.KEY_HOME:
            self._pad.y_shift = 0
        elif ch == curses.KEY_END:
            if self.__total_height < self._pad.height:
                self._pad.y_shift = 0
            else:
                self._pad.y_shift = self.__total_height - self._pad.height
        elif ch == curses.KEY_PPAGE:
            new_pos = self._pad.y_shift - self._pad.height
            if new_pos >= 0:
                self._pad.y_shift = new_pos
            else:
                self._pad.y_shift = 0
        elif ch == curses.KEY_NPAGE:
            if self.__total_height < self._pad.height:
                self._pad.y_shift = 0
            else:
                new_pos = self._pad.y_shift + self._pad.height
                if new_pos + self._pad.height < self.__total_height:
                    self._pad.y_shift = new_pos
                else:
                    self._pad.y_shift = self.__total_height - self._pad.height
        elif ch == int(self._main_window.settings.get(u'Keycodes', u'KEY_SHIFT_LEFT', False) or -1):
            self._pad.x_shift -= 16
            if self._pad.x_shift < 0:
                self._pad.x_shift = 0
        elif ch == int(self._main_window.settings.get(u'Keycodes', u'KEY_SHIFT_RIGHT', False) or -1):
            self._pad.x_shift += 16
            if self._pad.x_shift + self._pad.width > self.__total_width:
                new_shift = self.__total_width - self._pad.width
                if new_shift < 0:
                    new_shift = 0
                self._pad.x_shift = new_shift
        else:
            return
        self._pad.draw()
        self._window.refresh()
